package hebClustering.documentTypes;
import hebClustering.nlp.Corpus;
import hebClustering.searchEngines.WebsiteInformation;

import java.util.HashMap;
import java.util.Set;


/**
 * This class represents a document, i.e. a set of words.
 *
 */
public class Document {

	private int totalTokens;
	private HashMap<String,Integer> numOfRepetitions;
	private WebsiteInformation info;
	
	/**
	 * Creates an empty document.
	 * 
	 * @param info - A data structure that hold information about the document.
	 * Used mainly for Internet pages to store their title, description and URL.
	 */
	public Document(WebsiteInformation info){
		this.info = info;
		totalTokens = 0;
		numOfRepetitions = new HashMap<String, Integer>();
	}

	/**
	 * Checks if a token is a part of the document.
	 * 
	 * @param token	- A word.
	 * 
	 * @return true if the document contains the token, otherwise false.
	 */
	public boolean contains(String token){
		return numOfRepetitions.containsKey(token);
	}

	
	/**
	 * Returns the document URL path.
	 * 
	 * @return          Document's URL path.
	 */
	public String getPath(){
		return info.getUrl();
	}

	/**
	 * Returns the number of tokens which the document contains.
	 * 
	 * @return          Number of tokens in the document.
	 */
	public int getTotalNumOfTokens(){
		return totalTokens;
	}

	/**
	 * Returns the number of occurrences of a token in the document.
	 * 
	 * @param token - A word.	
	 * @return          The number of times the token appears in the document.
	 */
	public int getNumOfRepetitions(String token){
		return numOfRepetitions.get(token); 
	}
	
	/**
	 *	Add a new token to the document.
	 * 
	 * @param text - A word.
	 * @param num - Number of occurrences in the document.
	 */
	public void addToken(String text, int num) {
		totalTokens++;
		numOfRepetitions.put(text, num);
		Corpus.getCorpus().addToken(text);
	}

	/**
	 *	Returns the set of tokens which compose the document.
	 *
	 * @return         	A set with all the words in the document.
	 */
	public Set<String> getWordSet() {
		return numOfRepetitions.keySet();
	}
	
	/**
	 * Return the information of the document.
	 * 
	 * @return A data structure that hold information about the document.
	 */
	public WebsiteInformation getInfo() {
		return info;
	}
	
	/**
	 * Sets the information of the document.
	 * 
	 * @param info - A data structure that hold information about the document.
	 */
	public void setInfo(WebsiteInformation info) {
		this.info = info;
	}

	/**
	 * Returns a string representation.
	 *
	 * @return a string representation of the cluster set.
	 */
	public String toString(){
		StringBuffer sBuffer = new StringBuffer(getPath()+"\n");
		
		for(String token : numOfRepetitions.keySet())
			sBuffer.append(token + "=" + numOfRepetitions.get(token) + "\n");
			
		return sBuffer.toString();
	}

	

	

}
